package cn.ekuma.data.ui.android;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import cn.ekuma.data.dao.bean.I_ViewBean;

public abstract class AbstractViewBeanListAdapter<T extends I_ViewBean> extends ArrayAdapter<T>  {

	public static final int INVALID_POSITION = -1;
	
	private int  selectItem=INVALID_POSITION; 
	
	public AbstractViewBeanListAdapter(Context context) {
		super(context, 0, new ArrayList<T>());
	}
	
	public AbstractViewBeanListAdapter(Context context, List<T> objects) {
		super(context, 0, objects);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		  
	        //View view = LayoutInflater.from(getContext()).inflate( R.layout.list_item, null);

		    View view = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null);
		
	        TextView text = (TextView) view;
	        T item = getItem(position);
            if(item==null)
                text.setText("空");
            else
               text.setText(cracteBaseBeanToShowText(item));
            
	        if (position == selectItem) {    
	        	view.setBackgroundColor(Color.YELLOW);    
            }else {    
            	view.setBackgroundColor(Color.TRANSPARENT);    
            }       

	         return view;
	}

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }

    public abstract String cracteBaseBeanToShowText(T obj);

	public int getSelectItem() {
		return selectItem;
	}

	public void setSelectItem(int selectItem) {
		this.selectItem = selectItem;
	}
 
	public void clearSelect(){
		 selectItem=INVALID_POSITION;
	}

	public T getSelectBean() {
		if(selectItem!=INVALID_POSITION)
			return this.getItem(selectItem);
		return null;
	}
	
	
	public void moveUp(){
		
		System.out.println("向上移之前："+selectItem);
		if(selectItem!= AbstractViewBeanListAdapter.INVALID_POSITION&&selectItem>0){
		  --selectItem;
		  System.out.println("向上移之后："+selectItem);
		}
		notifyDataSetInvalidated();
	};
	
	public void moveDown(){
		System.out.println("向下移之前："+selectItem);
		if(selectItem<this.getCount()-1){
			++selectItem;
		    System.out.println("向下移之后："+selectItem);
		}	
	  notifyDataSetInvalidated();
	}

	@Override
	public void clear() {
		super.clear();
		clearSelect();
	}
	
	
	public void remove(int position) {
		remove(getItem(position));
	}

}
